/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import java.sql.ResultSet;
import mapping.SqlLib;
import entreprise.UserPatient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Quoc-Khai
 */
public class DBUserPatient {
    public UserPatient getUser(String userName){
        UserPatient user=null;
        String selectQuery;
        selectQuery = "SELECT * FROM `user-patient` WHERE userName='"+userName+"'";        
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);
            try {
                while(sqlResultSet.next()) {
                    user=new UserPatient(sqlResultSet.getString("patientID"), sqlResultSet.getString("userName"), sqlResultSet.getString("userPass"));
                }
            } catch (SQLException ex) {
                Logger.getLogger(DBUserPatient.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            return user;
        } 
        //TODO;
        return user;
    }
    public boolean createUser(UserPatient user){
        String insertQuery;
        insertQuery = "INSERT INTO `user-patient` (patientID,userName,userPass) VALUES (" + user.getPatientID()
                + ",'" + user.getUserName()+ "','" + user.getUserPass()+ "')";
        SqlLib connec = new SqlLib();        
        if (connec.initConnection()) {
            connec.execQuery(insertQuery);
            return true;
        } 
        return false;
    }
    public boolean updateUser(UserPatient user){
        //TODO
        return false;
    }
    public boolean deleteUser(String userName){
        String deleteQuery;
        deleteQuery = "DELETE FROM `user-patient` WHERE userName='"+userName+"'"; 
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(deleteQuery);
            return true;
        }       
        return false;
    }    
    public ArrayList<UserPatient> getUsers(String conditions){
        ArrayList<UserPatient> users=new ArrayList<UserPatient>();       
        String selectQuery="select * from `user-patient` order by patientID desc";
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);            
            try {
                while(sqlResultSet.next()) {
                    UserPatient userPatient=new UserPatient(sqlResultSet.getString("patientID"),sqlResultSet.getString("userName"),sqlResultSet.getString("userPass"));
                    users.add(userPatient);
                }
            } catch (SQLException ex) {
                Logger.getLogger(DBDrug.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
//            System.out.println("Drug not found!");
        } 
        return users;
    }    
}
